package com.gaoyi.crm.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.result.ActivityResult;

import com.gaoyi.crm.R;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.BundleKeyName;
import com.gaoyi.crm.databinding.FragmentResultBinding;
import com.gaoyi.crm.db.bean.ResultBean;
import com.gaoyi.crm.fragment.base.BaseViewFragment;
import com.gaoyi.crm.fragment.ctm.CtmDateFragment;
import com.gaoyi.crm.util.Utils;

import java.util.Map;

public class ResultFragment extends BaseViewFragment<FragmentResultBinding> {

    private String mFrom;

    @Override
    protected FragmentResultBinding getViewBinding() {
        return FragmentResultBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            ResultBean bean = bundle.getParcelable(BundleKeyName.RESULT);
            if (bean == null) {
                return;
            }
            mFrom = bean.getFrom();
            mBinding.tvRemark.setText(bean.getRemark());
            if (bean.getResult() == ApiStatus.FAIL) { // 失败
                failView(bean.getTip());
            } else { // 成功
                successView();
            }
        }
        mBinding.btnConfirm.setOnClickListener(v -> confirm());
    }

    private void confirm() {
        if (TextUtils.isEmpty(mFrom)) {
            mContext.onBackPressed();
        }
        if (mFrom.equals(ScannedListFragment.class.getSimpleName())) { // 返回到Home Page页
            Utils.backToHomePage(mController);
        } else if (mFrom.equals(CtmDateFragment.class.getSimpleName())) { // Go to synced list
            mController.navigate(R.id.action_resultFragment_to_ctmSyncedFragment);
        } else {
            mContext.onBackPressed();
        }
    }

    /**
     * 当create new promotion完成后，按返回键回到home page
     */
    public boolean onBackPressed() {
        if (TextUtils.isEmpty(mFrom)) {
            return false;
        }
        if (mFrom.equals(ScannedListFragment.class.getSimpleName())) { // 返回到Home Page页
            Utils.backToHomePage(mController);
            return true;
        } else {
            return false;
        }
    }

    private void successView() {
        mBinding.ivResult.setImageResource(R.drawable.successfully);
        mBinding.tvResult.setText(R.string.successfully);
        mBinding.tvFailRemark.setVisibility(View.GONE);
    }

    private void failView(String tip) {
        mBinding.ivResult.setImageResource(R.drawable.failed);
        mBinding.tvResult.setText(R.string.failed);
        if (!TextUtils.isEmpty(tip)) {
            mBinding.tvFailRemark.setText(tip);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }

}